import Request from 'utils/request';

import moment from 'moment';

export default class LoginApi {
  static toLogin(login) {
    const apiName = 'login/loginForToken';

    let postData = login.getObject();
    return Request.post(apiName, postData).then(res => {
      let data = res.data;
      data.systemTime = res.systemTime
      return data;
    });
  }

  static qrcodeLogin(code) {
    const apiName = 'api/wechatopen/getUserLoginByScanQrcode';

    return Request.post(apiName, {
      code
    }).then(res => {
      let data = res.data;
      if (data === null) {
        return null;
      }
      data.systemTime = res.systemTime
      return data;
    });
  }

  static getUserData() {
    const apiName = 'userPermission/getUserPermissionVue';

    return Request.post(apiName).then(res => {
      let menus = res.data;
      return menus;
    });
  }

  static getTocken(refreshToken) {
    const apiName = 'login/loginForRefreshToken';
    return Request.post(apiName, {
      refreshToken
    }, false).then(res => {
      return res.data;
    });
  }

  static logOutSystem() {
    const apiName = 'login/logoutForSession';
    return Request.post(apiName).then(res => {
      return res;
    });
  }

  static updatePassword(passwordData) {
    const apiName = 'api/user/passwordUpdateLogin';
    let postData = passwordData.getObject();
    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }

  static resetPassword(passwordData) {
    const apiName = 'api/user/passwordUpdate';
    let postData = passwordData.getObject();
    return Request.post(apiName, postData, true, false).then(res => {
      return res.msg;
    });
  }

  static getInspirationalText() {
    const apiName = 'api/goodmorning/randOne';
    return Request.post(apiName).then(res => {
      let text = res.data.text;
      let time = res.systemTime;
      time = moment(time).valueOf();
      return {
        text,
        time
      };
    });
  }
}
